package com.briup.web.controller;

import com.briup.bean.Shop;
import com.briup.bean.User;
import com.briup.dao.IBrowsingHistoryDao;
import com.briup.service.IBrowsingHistoryService;
import com.briup.service.ICollectService;
import com.briup.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
public class ViewShopController {
    @Autowired
    private IShopService shopService;
    @Autowired
    private IBrowsingHistoryService  browsingHistoryService;
    @GetMapping(value = {"/toViewShop","/ViewShop"})
    public String toViewShop(long shopId, Model model, HttpSession session){
        shopService.updateVisitVolume(shopId);

        User user = (User) session.getAttribute("user");
        if(user!=null){
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//注意月和小时的格式为两个大写字母
            java.util.Date date = new Date();//获得当前时间
            browsingHistoryService.saveUserBrowsingHistory(user.getId(),shopId,df.format(date));
            //System.out.println(browsingHistoryDao.getUserBrowsingHistory(user.getId()));
        }
        Shop shop = shopService.findShopById(shopId);
        model.addAttribute("shop",shop);
        return "viewShop";
    }


}
